<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/4/21
 * Time: 15:37
 * Content:请求用户所有信息
 */

namespace app\index\controller;


use Redis\Redis;
use think\Db;

class CarInfo extends Common
{

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 车辆绑定的用户列表
     */
    function carBindingInfo()
    {
        # 接收车牌号
        $carNum = input('post.car_num');
        if (!empty($carNum)) {
            # 查询车辆绑定的用户信息
            $info = Db::table('member_car mc')
                ->field('distinct mc.member_id user_id,
                if(member_name is null or member_name = "",from_base64(nickname),member_name) user_name,member_phone phone')
                ->join('member m', 'm.id=mc.member_id', 'left')
                ->where('mc.member_id is not null and mc.member_id != 0 and mc.status != 3')
                ->where(array('car_licens' => $carNum))
                ->select();
            return array('status' => true, 'data' => $info);
        } else {
            return array('status' => false, 'msg' => '车牌号为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 请求车辆详情信息
     */
    function getCarDetail()
    {
        #获取车牌号
        $carNum = input("post.car_num");
        #获取用户id
        $userId = input("post.user_id");
        if (!empty($userId)) {
            if (CheckMobile($userId)) {
                $userId = Db::table('member')->field('id')->where(array('member_phone' => $userId))->value('id');
                if (empty($userId)) {
                    return array('status' => false, 'msg' => '用户信息不存在');
                }
            }
            if (empty($carNum)) {
                $carNum = Db::table('member_car')->field('car_licens')->where(array('member_id' => $userId))->order('id asc')->value('car_licens');
            }
            #member,member_car,member_level member_mapping  车俩型号id 为 car_sort  的id  显示:car_log 的title - car_sort 的 sort_title  即 title-sort_title
            if (empty($carNum)) {
                $carInfo = Db::table('member m')
                    ->field("if(m.member_name is null or m.member_name = '',from_base64(nickname),member_name) user_name,m.member_phone phone,mm.member_level_id member_level,
                     if(mm.register_time is null,mm.member_create,mm.register_time) register_time, mm.member_expiration, mm.member_integral ,mm.member_integral integral_balance,mm.member_balance, date_format( mm.register_time,'%Y.%m.%d %H:%i:%s') login_time,
                     (select ml.level_title from member_level ml where ml.id = (select mm.member_level_id from member_mapping mm where mm.member_id = $userId )) member_grade,
                     if(mm.member_type=1,'不是','是') partner
                     ")
                    ->join('member_mapping mm', 'm.id=mm.member_id', 'left')
                    ->where(array('m.id' => $userId))
                    ->find();
            } else {
                $carInfo = Db::table('member_car mc')
                    ->field("if(m.member_name is null or m.member_name = '',(nickname),member_name) user_name,m.member_phone phone,
                cl.title,cs.sort_title,
                date_format( mc.register_time,'%Y.%m.%d %H:%i:%s') login_time,
                mc.car_mileage,
                (select if(mm.member_type=1,'不是','是') from member_mapping mm where mm.member_id = $userId ) partner,
                
                (select if(mm.register_time is null,mm.member_create,mm.register_time) register_time from member_mapping mm where mm.member_id = $userId ) register_time,
                (select mm.member_expiration from member_mapping mm where mm.member_id = $userId ) expiration_time,
                (select mm.member_integral from member_mapping mm where mm.member_id = $userId ) integral_balance,
                (select mm.member_balance from member_mapping mm where mm.member_id = $userId ) member_balance,
                (select mm.member_level_id from member_mapping mm where mm.member_id = $userId ) member_level,
                (select ml.level_title from member_level ml where ml.id = (select mm.member_level_id from member_mapping mm where mm.member_id = $userId )) member_grade
                ")
                    ->join('member m', 'mc.member_id = m.id')
                    ->join('car_sort cs', 'cs.id = mc.car_type_id')
                    ->join('car_logo cl', 'cl.id = cs.logo_id')
                    ->where("mc.member_id = '$userId' and mc.car_licens = '$carNum' ")
                    ->find();
            }
            if (!empty($carInfo)) {
                if (!empty($carInfo['title'])) {
                    $carInfo['brand_model'] = $carInfo['title'] . "-" . $carInfo['sort_title'];
                } else {
                    $carInfo['brand_model'] = '-';
                }
                if (empty($carInfo['member_grade']))
                    $carInfo['member_grade'] = '普通用户';
                if ($carInfo['member_level'] > 1) {
                    $carInfo['expiration_time'] = '永久有效';
                }
                if ($carInfo['member_level'] == 0) {
                    $carInfo['expiration_time'] = '';
                }
                unset($carInfo['title'], $carInfo['sort_title']);
                return array('status' => true, 'msg' => '查询成功', 'data' => $carInfo, 'user_id' => $userId);
            } else {
                return array('status' => false, 'msg' => '查询结果为空', 'data' => array());
            }
        } else {
            return array('status' => false, 'msg' => '车牌号或用户id为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 车辆服务记录
     */
    function getServiceRecord()
    {
        $memberId = input("post.user_id");
        if (!empty($memberId)) {
            //ls.log_service_price price,
            //if(o.order_price > 0 and o.order_price is not null,o.order_price ,((select COALESCE(sum(price),0) from order_server where order_number = ls.order_number)+(select COALESCE(sum(pro_price),0) from order_biz where order_number = ls.order_number)))
            $service = Db::table('log_service ls')
                ->field('ls.order_number,car_licens car_num,date_format(log_service_create,\'%Y-%m-%d %H:%i\') time ,o.id order_id,
                 o.pay_price price,
                case o.pay_type 
                            when 1 then \'微信支付\'
                            when 2 then \'支付宝支付\'
                            when 3 then \'银联支付\' 
                            when 4 then \'现金支付\'
                            when 5 then \'余额支付\'
                            when 6 then \'卡券支付\' end pay_type,
                if(o.member_car_id=0,(select sort_title from car_sort cs where cs.id = o.car_sort),(select sort_title from car_sort cs right join member_car mc on mc.car_type_id = cs.id where mc.id = o.member_car_id)) sort_title,
if(o.member_car_id=0,(select title from car_logo cl where cl.id = o.car_logo),(select title from car_logo cl right join car_sort cs on cs.logo_id = cl.id right join member_car mc on mc.car_type_id = cs.id where mc.id = o.member_car_id)) logo_title,
if(o.member_car_id=0,(select level_title from car_level cl where cl.id = o.car_level),(select level_title from car_level cl right join car_sort cs on cs.level = cl.id right join member_car mc on mc.car_type_id = cs.id where mc.id = o.member_car_id)) carType')
                ->join('orders o', 'o.order_number=ls.order_number', 'left')
                ->where(array('ls.member_id' => $memberId))//, 'ls.biz_id' => $this->bizId
                ->group('order_number,car_licens')
                ->order('log_service_create desc')
                ->select();
            if (!empty($service)) {
                return array('status' => true, 'data' => $service);
            } else {
                return array('status' => false, 'msg' => '服务记录为空', 'data' => array());
            }
        } else {
            return array('status' => false, 'msg' => '用户id为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 服务记录详情
     */
    function getServicesDetail()
    {
        $order_number = input("post.order_number");
        $_return = array();
        # 查询订单支付方式、实际支付金额、结算时间
        $orders = Db::table("orders o")
            ->field("order_over,pay_type,pay_price,order_price,voucher_price,biz_title,
            if(o.order_price > 0 and o.order_price is not null,o.order_price ,((select COALESCE(sum(price),0) from order_server where order_number = o.order_number)+(select COALESCE(sum(pro_price),0) from order_biz where order_number = o.order_number))) order_price")
            ->join('biz b', 'b.id =o.biz_id', 'left')
            ->where(array("order_number" => $order_number))
            ->find();
        if (!empty($orders)) {
            if ($orders['pay_type'] == 6) {
                $orders['pay_price'] = "0.00";
            }
            $_return['data']['order_over'] = $orders['order_over'];
            $_return['data']['biz_title'] = $orders['biz_title'];
            $_return['data']['pay_price'] = $orders['pay_price'];
            $_return['data']['pay_type'] = getPayType($orders['pay_type']);
            # 查询商品工单
            $orderBiz = Db::table('order_biz ob')
                ->field("ob.id,'" . $orders['order_over'] . "' time,ob.pro_price price,ob.bizpro_number num,
                if(ob.custommark=1,'系统商品','自定义商品') type,
                if(ob.custommark = 1,(select biz_pro_title from biz_pro bp where bp.id = ob.biz_pro_id limit 1),
                (select custom_biz_title from custom_biz cb where cb.id = ob.biz_pro_id limit 1)) server_title,
                if(ob.custommark = 1,(select class_title from pro_class pc where id = (select biz_pro_class_id from biz_pro bp where bp.id = ob.biz_pro_id limit 1)),
                (select class_title from pro_class where id = (select custom_biz_class from custom_biz cb where cb.id = ob.biz_pro_id limit 1))) class_title
                ")
                ->where(array('order_number' => $order_number))
                ->buildSql();
            # 查询服务工单
            $orderServer = Db::table('order_server os')
                ->field("os.id,os.order_end time,os.price,1 num,
                if(os.custommark=1,'系统服务','自定义服务') type,
                if(os.custommark=1,(select service_title from service s where s.id=os.server_id limit 1),
                   (select custom_server_title from customserver cs where cs.id=os.server_id limit 1)) server_title,
                   if(os.custommark=1,(select service_class_title from service_class where id = (select service_class_id from service s where s.id=os.server_id limit 1)),
                   (select service_class_title from service_class where id = (select custom_server_class from customserver cs where cs.id=os.server_id limit 1))) class_title
                ")
                ->where(array('order_number' => $order_number))
                ->union([$orderBiz])
                ->buildSql();
            $orderDetail = Db::table($orderServer . 'tem')->select();
            $coupon = 0;// 定义使用卡券金额
            # 定义返回值
            $discount_text = '';
            # 查询该订单的卡券使用总金额
            $logCashCoupon = Db::table('log_cashcoupon')->where(array('order_number' => $order_number))->sum('voucher_price');
            if ($logCashCoupon > 0) {
                $discount_text = '卡券核销' . $logCashCoupon . '元';
            }
            $_return['data']['discount_text'] = $discount_text;
            $_return['data']['total_price'] = $orders['order_price'];
            $_return['data']['voucher_price'] = $logCashCoupon;
            $_return['data']['service_list'] = $orderDetail;
            $_return['status'] = true;
        } else {
            $_return['status'] = false;
        }
        return $_return;
    }

    /**
     * @return array
     * @content 用户消费记录
     */
    function getConsumeRecord()
    {
        $memberId = input("post.user_id");
        if (!empty($memberId)) {
            $info = Db::query("select id,format(consump_price,2) price,date_format(consump_time,'%Y-%m-%d %H:%i') as `time`,
 case consump_pay_type 
      when 5 then '余额支付'
      when 1 then '微信支付'
      when 2 then '支付宝支付'
      when 3 then '银联支付'
      when 4 then '现金支付'
end pay_type
 from log_consump where log_consump_type = 1 and member_id = " . $memberId . " order by consump_time desc");
            if (!empty($info)) {
                return array('status' => true, 'data' => $info);
            } else {
                return array('status' => false, 'msg' => '消费记录为空', 'data' => array());
            }
        } else {
            return array('status' => false, 'msg' => '用户id为空');
        }
    }

    /**
     * @return array
     * @content 用户充值记录
     */
    function getRechargeRecord()
    {
        $memberId = input("post.user_id");
        if (!empty($memberId)) {
            $info = Db::query("select id,format(consump_price,2) price,date_format(consump_time,'%Y-%m-%d %H:%i') as `time`,
 case consump_pay_type 
      when 5 then '余额支付'
      when 1 then '微信支付'
      when 2 then '支付宝支付'
      when 3 then '银联支付'
      when 4 then '现金支付'
end pay_type,
case income_type 
      when 2 then '办理会员'
      when 6 then '用户升级'
      when 7 then '用户充值'
end income_type
 from log_consump where income_type in (2,6,7) and member_id = " . $memberId . " order by consump_time desc");
            if (!empty($info)) {
                return array('status' => true, 'data' => $info);
            } else {
                return array('status' => false, 'msg' => '充值记录为空', 'data' => array());
            }
        } else {
            return array('status' => false, 'msg' => '用户id为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 用户绑定的车辆列表
     */
    function getBindCarList()
    {
        $memberId = input("post.user_id");
        $mark = input('post.mark');
        if (!empty($memberId)) {

            if(!empty($mark)) {
                # 用户车辆列表(车辆切换弹窗)
                $carInfo = Db::table("member_car mc")
                    ->field("mc.*,cl.title logo_title,cs.sort_title,cle.level_title,cs.level,cs.logo_id,date_format( mc.register_time,'%Y.%m.%d %H:%i:%s') login_time")
                    ->join("car_sort cs", "mc.car_type_id=cs.id", 'left')
                    ->join("car_logo cl", "cs.logo_id=cl.id", 'left')
                    ->join("car_level cle", "cs.level=cle.id", 'left')
                    ->order(array('queue_status' => 'asc'))
                    ->where(array("mc.member_id" => $memberId))->where("mc.status!=3")
                    ->order('mc.id asc')->select();
                if(!empty($carInfo)){
                    foreach ($carInfo as $k=>$v){
                        $carInfo[$k]['title_info'] = $v['logo_title'] . '·' . $v['sort_title'] . "(" . $v['level_title'] . ")";
                    }
                }
            }else{
                $carInfo = Db::table('member_car mc')
                    ->field('cl.title brand_model,cs.sort_title,cle.level_title,mc.car_licens car_num,mc.car_mileage,mc.car_recogin_code')
                    ->join('car_sort cs', 'cs.id=mc.car_type_id', 'left')
                    ->join('car_logo cl', 'cl.id=cs.logo_id', 'left')
                    ->join('car_level cle', 'cle.id=cs.level', 'left')
                    ->where(array('mc.member_id' => $memberId))
                    ->where('mc.status != 3')
                    ->order('mc.id asc')
                    ->select();
            }
            if (!empty($carInfo)) {
                return array('status' => true, 'data' => $carInfo);
            } else {
                return array('status' => false, 'msg' => '车辆列表为空', 'data' => array());
            }
        } else {
            return array('status' => false, 'msg' => '用户id为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 修改车辆VIN码
     */
    function writeVIN()
    {
        # 接收车牌号
        $car_num = input('post.car_num');
        if (empty($car_num)) {
            return array('status' => false, 'msg' => '车牌号不能为空');
        }
        # 接收VIN码
        $car_recogin_code = input('post.car_recogin_code');
        if (empty($car_recogin_code)) {
            return array('status' => false, 'msg' => '车辆VIN码不能为空');
        }
        Db::table('member_car')->where(array('car_licens' => $car_num))->update(array('car_recogin_code' => $car_recogin_code));
        return array('status' => true, 'msg' => '修改成功');
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 请求卡券记录
     */
    function getCardVoucher()
    {
        $memberId = input("post.user_id");
        if (!empty($memberId)) {
            #查询红包详情
            //(rd.voucher_number-(select count(id) from log_cashcoupon as lc where lc.member_id = " . $member_id . " and lc.voucher_id = mv.voucher_id )) num
            $redbag = Db::table('member_voucher mv')
                ->field("mv.voucher_id,card_title card_name,format(voucher_cost,2) price,voucher_type,mv.create expiration_time,money_off,redpacket_id,voucher_type,
                1 num")
                ->where("member_id = $memberId and mv.status = 1")
                ->where(" (redpacket_id != 0)  ")
                ->where("mv.create > '" . date('Y-m-d H:i:s') . "'")
                ->group('mv.redpacket_id')
                ->select();
            if (!empty($redbag)) {
                foreach ($redbag as $k => $v) {
                    $redbag[$k]['type'] = getTypeTitle($v['voucher_type']);
                    $redbag[$k]['num'] = Db::table('member_voucher')
                        ->where("( redpacket_id != 0)  and status = 1")
                        ->where("`create` > '" . date('Y-m-d H:i:s') . "'")
                        ->where(array('redpacket_id'=>$v['redpacket_id'],'member_id'=>$memberId,'voucher_type'=>$v['voucher_type']))
                        ->count();
                }
            }
            #查询卡券详情
            $ordinary = Db::table('member_voucher mv')
                ->field('mv.voucher_id,card_title name,format(voucher_cost,2) price,mv.create expiration_time,0 money_off,voucher_type,voucher_id,voucher_type,1 num')
                ->where("(voucher_id != 0 ) ")
                ->where("mv.create > '" . date('Y-m-d H:i:s') . "'")
                ->where(" mv.status = 1 and mv.member_id = " . $memberId)
                ->group('mv.voucher_id')
                ->select();
            if (!empty($ordinary)) {
                foreach ($ordinary as $k => $v) {
                    $ordinary[$k]['type'] = getTypeTitle($v['voucher_type']);
                    $ordinary[$k]['num'] = Db::table('member_voucher')
                        ->where(array('voucher_id'=>$v['voucher_id'],'member_id'=>$memberId))
                        ->where("(voucher_id != 0 ) and voucher_type = ".$v['voucher_type']."  and status = 1")
                        ->where("`create` > '" . date('Y-m-d H:i:s') . "'")
                        ->count();
                }
            }
            $info = array('ordinary' => $ordinary, 'redbag' => $redbag);
            return array('status' => true, 'data' => $info);


        } else {
            return array('status' => false, 'msg' => '用户id为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 车辆保养记录
     */
    function logMaintain()
    {
        # 接收用户id
        $member_id = input('post.member_id');
        if (empty($member_id)) {
            return array('status' => false, 'msg' => '用户id不能为空');
        }
        # 查询该车辆的保养记录
        $info = Db::table('member_car mc')
            ->field('mc.car_licens car_num,(CONCAT_WS("-",cl.title,cs.sort_title)) car_info,cle.level_title
                ')
            ->join('car_sort cs', 'cs.id=mc.car_type_id', 'left')
            ->join('car_logo cl', 'cl.id=cs.logo_id', 'left')
            ->join('car_level cle', 'cle.id=cs.level', 'left')
            ->where(array('mc.member_id' => $member_id))
            ->select();
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $logMaintain = Db::table('log_maintain')->field('maintain_type,now_mileage,
                date_format(maintain_time,"%Y-%m-%d %H:%i") maintain_time')->where(array('car_licens' => $v['car_num']))->order(array('maintain_time' => 'desc'))->find();
                //$typeInfo = getsMaintainTypeInfo($logMaintain['maintain_type']);
                if (!empty($logMaintain)) {
                    $typeInfo = getsMaintainTypeInfo($logMaintain['maintain_type']);
                    $info[$k]['maintain_title'] = $typeInfo['title'];
                    $info[$k]['maintain_time'] = $logMaintain['maintain_time'];
                    $info[$k]['now_mileage'] = $logMaintain['now_mileage'];
                } else {
                    unset($info[$k]);
                }
            }
        }
        return array('status' => true, 'data' => $info);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 保养记录详情
     */
    function logMaintainDetail()
    {
        # 接收车牌号
        $carNum = input('post.car_num');
        if (empty($carNum)) {
            return array('status' => false, 'msg' => '车牌号不能为空');
        }
        $info = Db::table('log_maintain lm')
            ->field('maintain_type,maintain_price,now_mileage,next_mileage,remark,maintain_price,
                date(maintain_time) maintain_time,suggest_time')
            ->where(array('car_licens' => $carNum))
            ->order('lm.id desc')
            ->select();
        $_returnArr = array();
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                if (array_key_exists($v['maintain_time'] . '-' . $v['now_mileage'], $_returnArr)) {
                    # 合并
                    if (array_key_exists($v['maintain_type'], $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['list'])) {
                        # 数量加一
                        $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['list'][$v['maintain_type']]['num'] += 1;
                    } else {
                        # 新增
                        $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['list'][$v['maintain_type']] = array(
                            'maintain_type' => $v['maintain_type'],
                            'num' => 1,
                            'price' => $v['maintain_price'],
                            'maintain_title' => getsMaintainTypeInfo($v['maintain_type'])['title']
                        );
                    }
                } else {
                    # 新增
                    $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['maintain_time'] = $v['maintain_time'];
                    $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['now_mileage'] = $v['now_mileage'];
                    $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['next_mileage'] = $v['next_mileage'];
                    $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['suggest_time'] = $v['suggest_time'];
                    $_returnArr[$v['maintain_time'] . '-' . $v['now_mileage']]['list'][$v['maintain_type']] = array(
                        'maintain_type' => $v['maintain_type'],
                        'num' => 1,
                        'price' => $v['maintain_price'],
                        'maintain_title' => getsMaintainTypeInfo($v['maintain_type'])['title']
                    );
                }
            }
        }
        return array('status' => true, 'info' => $_returnArr);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 获取车辆品牌信息
     */
    function getCarLogo()
    {
        # 接收查询信息
        $_search = input('post.search');
        if (!empty($_search)) {
            # 带搜索条件,直接查库
            $info = Db::table('car_logo cl')->field('cl.id logo_id,cl.title logo_title')
                ->join('car_sort cs', 'cs.logo_id=cl.id')
                ->where("cs.sort_title like '%" . $_search . "%' or cl.title like '%" . $_search . "%'")
                ->group('cl.id')
                ->select();
            $_return = groupByInitials($info, 'logo_title');
        } else {
            # 查询车辆品牌信息(先从redis中获取,redis中没有,查询数据库)
            $redis = new Redis();
            $_return = $redis->hGetJson('Ecars', 'carLogo');
            if (empty($_return)) {
                # redis中为空,查询数据库
                $info = Db::table('car_logo')->field('id logo_id,title logo_title')->select();
                $_return = groupByInitials($info, 'logo_title');
                $redis->hSetJson('Ecars', 'carLogo', $_return);
            }
        }
        return array('status' => true, 'data' => $_return);
    }


    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 获取车辆系列信息
     */
    function getCarSort()
    {
        # 接收品牌id
        $logoId = input('post.logo_id');
        if (!empty($logoId)) {
            # 先查redis,没有再查数据库
            $redis = new Redis();
            $_return = $redis->hGetJson('Ecars', 'carSort' . $logoId);
            if (empty($_return)) {
                # 查询数据库
                $_return = Db::table('car_sort cs')
                    ->field('cs.id sort_id,sort_title,level car_level,level_title')
                    ->join('car_level cl', 'cl.id=cs.level', 'left')
                    ->where(array('logo_id' => $logoId))
                    ->select();
                $redis->hSetJson('Ecars', 'carSort' . $logoId, $_return);
            }
            return array('status' => true, 'data' => $_return);
        } else {
            return array('status' => false, 'msg' => '品牌id为空');
        }
    }


    /**
     * @return array
     * @content 门店会员
     */
    function bindMember()
    {
        # 查询该门店所绑定的会员信息
        $info = Db::query("SELECT lhd.member_id,if(m.`member_name` IS NOT NULL ,m.`member_name` ,m.`nickname` ) member_name,m.`headimage` ,mm.member_balance,level_title  FROM `log_handlecard` lhd 
left join `member_level` ml on ml.`id`=lhd.`level_id` 
left join `member_mapping`mm on mm.`member_id` = lhd.`member_id` 
left join `member` m on m.id=lhd.`member_id` 
where lhd.id = (SELECT max(id)  FROM `log_handlecard` lh where lh.`member_id` = lhd.`member_id` ) and `biz_id` = " . $this->bizId . " GROUP BY `member_id`");
        # 会员推荐人数
        $people = count($info);
        # 推荐总收益
        $income = Db::table('deduct')->where(array('biz_id' => $this->bizId, 'type' => 3))->sum('deduct');
        return array('status' => true, 'msg' => '查询成功', 'info' => $info, 'people' => $people, 'income' => $income);
    }
}
